package presentation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import control.CControl;

public class PVideoPanel extends JPanel implements Observer {
	private static final long serialVersionUID = 1L;
	private JLabel webcamImage = new JLabel(new ImageIcon());
	private CControl cControl;
	private BufferedImage noConImage = null;
	
	public PVideoPanel(CControl cControl) {
		this.setLayout(new BorderLayout());
		this.setBackground(Color.lightGray);
		this.setPreferredSize(new Dimension(640,480));
		this.cControl = cControl;
		
		try {
			noConImage = ImageIO.read(new File(cControl.getRootPath() + "img/nocon.png"));
		} catch (IOException e) {}
		
		this.add(webcamImage,BorderLayout.CENTER);
	}
	
	@Override
	public void update(Observable obs, Object arg) {
		if (arg instanceof BufferedImage) {
			((ImageIcon) webcamImage.getIcon()).setImage((BufferedImage) arg);
	    	this.updateUI();
		} else if (arg instanceof String) {
			if (noConImage != null) {
				((ImageIcon) webcamImage.getIcon()).setImage(noConImage);
		    	this.updateUI();
			}
			
			cControl.addError((String) arg);
		}
	}

}
